﻿using EHR18.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace Common
{
    public class LGParser : DataPaser, IDataPaser
    {

        public DateTime TestTime { get; set; }
        public double UpperValue { get; set; }
        public double LowerValue { get; set; }
        public double Value { get; set; }
        public bool Result { get; set; }
        public string Unit { get; set; }
        public LGParser(string fileName)
            : base(fileName)
        {
            List<string> ss = getLastRecord();
            TestTime = DateTime.Parse(ss[0]);
            Regex rgx = new Regex(@"[-]?\d+\.?\d*");
            var match = rgx.Match(ss[1]);
            if (match.Success)
            {
                LowerValue = double.Parse(match.Value);
                Unit = ss[1].Substring(match.Value.Length+1);
            }
            else
            {
                LowerValue = double.NaN;
            }

            match = rgx.Match(ss[2]);
            if (match.Success)
            {
                UpperValue = double.Parse(match.Value);
            }
            else
            {
                UpperValue = double.NaN;
            }
            match = rgx.Match(ss[3]);
            if (match.Success)
            {
                var a = double.Parse(match.Value);
                if (ss[3].Contains("E"))
                {
                    var v = ss[3].Substring(ss[3].IndexOf("E") + 1);
                    var d = double.Parse(v);
                    a *= Math.Pow(10, d)*1000000;
                }
                Value = a;
            }
            else
            {
                Value = double.NaN;
            }
            Result = ss[5].Contains("PASS");          
        }

        public List<string> getCorlumnNames()
        {
            List<string> slist = new List<string>();
            using (StreamReader sr = new StreamReader(FileName, Encoding.GetEncoding("gb2312")))
            {
                string line = sr.ReadLine().TrimEnd(new char[] { ',' });
                if (string.IsNullOrEmpty(line))
                    throw new Exception("没有读到标题数据");
                string[] titles = splitLine(line);
                foreach (string title in titles)
                {
                    slist.Add(title);
                }
            }
            return slist;
        }


        public List<string> getLastRecord()
        {
            List<string> slist = new List<string>();
            string line = ReadLastLine(FileName, 1, Encoding.Default).TrimEnd(new char[] { '\r', '\n', ',' });
            string[] fileds = splitLine(line);
            foreach (string filed in fileds)
            {
                slist.Add(filed);
            }
            return slist;
        }
        private string[] splitLine(string line)
        {
            string[] fileds = line.Split(new char[] { ',', '\t' });
            return fileds;
        }
    }

}
